// import Cookies from "js-cookie";
// import { encrypt, decrypt } from "@/utils/jsencrypt";
import { setStore } from "@/utils/store"

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" }
        ],
        password: [{ required: true, trigger: "blur", message: "密码不能为空" }]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCookie();
  },
  methods: {
    getCookie() {
      // const username = Cookies.get("username");
      // const password = Cookies.get("password");
      // const rememberMe = Cookies.get("rememberMe");
      // this.loginForm = {
      //   username: username === undefined ? this.loginForm.username : username,
      //   password:
      //     password === undefined ? this.loginForm.password : decrypt(password),
      //   rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      // };
    },
    capctchaCheckSuccess(params) {
      this.loginForm.code = params.captchaVerification;
      this.loading = true;
      // if (this.loginForm.rememberMe) {
      //   Cookies.set("username", this.loginForm.username, { expires: 30 });
      //   Cookies.set("password", encrypt(this.loginForm.password), {
      //     expires: 30
      //   });
      //   Cookies.set("rememberMe", this.loginForm.rememberMe, { expires: 30 });
      // } else {
      //   Cookies.remove("username");
      //   Cookies.remove("password");
      //   Cookies.remove("rememberMe");
      // }
      this.$store
        .dispatch("Login", this.loginForm)
        .then(() => {
          this.$router.push({ path: "/" }).catch(() => { });
        })
        .catch(() => {
          this.loading = false;
        });
    },
    handlePwd() {
      const form = { ...this.loginForm, ...{ grant_type: "password", scope: "server" } }
      this.$store
        .dispatch("pwdLogin", form)
        .then(() => {
          setStore({
            type: 1,
            name: "userName",
            content: this.loginForm.username
          })
          this.$store.dispatch('GetInfo').then(() => {
            this.$store.dispatch('getDict')
            this.$store.dispatch('GenerateRoutes').then(accessRoutes => {
              this.$router.addRoutes(accessRoutes)
              const type = this.$store.state.user.userInfo.userType
              this.$nextTick(() => {
                this.$router.push({ path: type === "0" ? "/screen" : "/chart-ship" })
              })
            })
          }).catch(err => {
            this.$store.dispatch('LogOut').then(() => {
              this.$message.error(err)
            })
          })
        })
        .catch(() => {
          this.loading = false;
        });
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          // this.$refs.verify.show();
          this.handlePwd()
        }
      });
    }
  }
}